package com.zm.demo.core.concurrent;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.*;

import static java.lang.System.out;

/**
 * @author zoum
 * @create 2019/6/18 18:53
 */
public class ThreadPoolExecutorDemo {

    public static void main(String[]args) throws Exception{
        run();
//        reject();
//        synQueue();
    }

    private static void run() throws Exception{
        ThreadPoolExecutor pool = new ThreadPoolExecutor(5, 9, 0, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<>(80));
        for (int i = 0; i < 70; i++) {
            pool.execute(() -> {
                try {
                    out.println(Thread.currentThread().getName() + "=== run1");
                    Thread.sleep(5000L);
                }catch (Exception e){

                }
            });
        }
        out.println("getActiveCount: " + pool.getActiveCount());
        pool.shutdown();
        ExecutorCompletionService ecs = new ExecutorCompletionService(pool);
        if (ecs.take().isDone()){

        }
//        pool.shutdownNow();
//        out.println("等待10秒是否完成：" + pool.awaitTermination(10L, TimeUnit.SECONDS));
//        out.println("等待100秒是否完成：" + pool.awaitTermination(100L, TimeUnit.SECONDS));
        out.println("等待100秒是否完成：" + pool.isTerminated());
        pool.shutdown();
        pool.shutdown();
        out.println("taskCount = " + pool.getTaskCount());
    }

    private static void reject(){
        ThreadPoolExecutor pool = new ThreadPoolExecutor(1, 1, 0, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(1), new RejectedExecutionHandler() {
            @Override
            public void rejectedExecution(Runnable r, ThreadPoolExecutor executor) {
                out.println("rejected taskCount = " + executor.getTaskCount());
                executor.setMaximumPoolSize(5);
            }
        });

//        ExecutorService pool = Executors.newCachedThreadPool();
        for (int i = 0; i < 10; i++) {
            pool.execute(() -> {
                try {
                    out.println(Thread.currentThread().getName() + "=== run");
                    Thread.sleep(1000L);
                }catch (Exception e){

                }
            });
        }
        out.println("getActiveCount: " + pool.getActiveCount());
        pool.shutdown();
    }

    private static void synQueue() throws InterruptedException {
        ThreadPoolExecutor pool = new ThreadPoolExecutor(5, 5, 0, TimeUnit.MILLISECONDS, new SynchronousQueue<>());
        List<Callable<Boolean>> list = new ArrayList<>();
        for (int i = 0; i < 10; i++) {
            list.add(()->{
                try {
                    out.println(Thread.currentThread().getName() + "=== run1");
                    Thread.sleep(10000L);
                }catch (Exception e){

                }
                return true;
            });
        }

        try {
            List<Future<Boolean>>futureList = pool.invokeAll(list);
            out.println("getActiveCount: " + futureList.size());
        } catch (Exception e){
            e.printStackTrace();
        }finally {
            pool.shutdown();
        }
    }
}
